package flc.ast.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.FormatAdapter;
import flc.ast.bean.MyFormatBean;
import flc.ast.databinding.DialogDnsBinding;
import flc.ast.databinding.DialogFormatBinding;
import stark.common.apis.ApiManager;
import stark.common.apis.base.IpAddrBean;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseSmartDialog;

public class FormatDialog extends BaseSmartDialog<DialogFormatBinding> implements View.OnClickListener {

    private String format;
    private FormatAdapter formatAdapter;
    private int oldPosition;

    public void setFormat(String format) {
        this.format = format;
    }

    public FormatDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_format;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.rvFormatList.setLayoutManager(new GridLayoutManager(getContext(), 2));
        formatAdapter = new FormatAdapter();
        mDataBinding.rvFormatList.setAdapter(formatAdapter);
        formatAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                formatAdapter.getItem(oldPosition).setSelected(false);
                oldPosition = position;
                formatAdapter.getItem(position).setSelected(true);
                formatAdapter.notifyDataSetChanged();
            }
        });

        List<MyFormatBean> list = new ArrayList<>();
        list.add(new MyFormatBean(getContext().getString(R.string.encode_text1), false));
        list.add(new MyFormatBean(getContext().getString(R.string.encode_text2), false));
        list.add(new MyFormatBean(getContext().getString(R.string.encode_text3), false));
        list.add(new MyFormatBean(getContext().getString(R.string.encode_text4), false));
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(format)) {
                list.get(i).setSelected(true);
                oldPosition = i;
                break;
            }
        }
        formatAdapter.setList(list);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvCancel:
            case R.id.tvConfirm:
                if (listener != null) {
                    listener.clickConfirm(formatAdapter.getItem(oldPosition).getName());
                }
                dismiss();
                break;
            default:
                break;
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(String format);
    }
}
